<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//首页数据
class Home extends CommonController {

	function __construct(){
		
		parent::__construct();
		
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_total(){
		$user_id = $this->user_id;
		
		$bz = $this->input->post('bz',null,'人民币');
		
		$this->load->model('User/CurrencyModel','CurrencyModel');
		$curinfo = $this->CurrencyModel->bz_get_info($bz);
		if(!$curinfo){
			$curinfo = array(
				'name'=>'人民币',
				'exchange_rate'=>1,
				'mark'=>'￥'
			);
		}
		
		
		$this->load->model('Chuna/cashiersModel','cashiersModel');
		$info = array();
		

		$info['cash'] = $this->get_total_cl(array('type'=>0),$curinfo['exchange_rate']);
		$info['bank'] = $this->get_total_cl(array('type'=>1),$curinfo['exchange_rate']);
		
		
		
		$this->load->model('User/companyModel','companyModel');
		//计算律所代收的
		$res = $this->companyModel->getAll(array('type'=>1));
		$money = 0;
		foreach($res as $val){
			$aa = $this->get_total_cl(array('company'=>$val['name'],'sx_name'=>$val['sx_name']),$curinfo['exchange_rate']);
			$money += $aa;
		}
		$info['lvsuo'] = $money;
		
		//计算风资的
		$res = $this->companyModel->getAll(array('type'=>0));
		$money = 0;
		foreach($res as $val){
			$aa = $this->get_total_cl(array('company'=>$val['name'],'sx_name'=>$val['sx_name']),$curinfo['exchange_rate']);
			
			$money += $aa;
		}
		$info['fengzi'] = $money;
		
		$info['yewu'] = $info['fengzi'] + $info['lvsuo'];
		
		
		$info['mark'] = $curinfo['mark'];
		
		$this->data_out($info);
	}
	
	function get_total_cl($arr,$exchange_rate="1"){
		
		$income_arr = $arr;
		$income_arr['mtype'] = 0;
		$income = $this->cashiersModel->get_totals($income_arr,'income');
		
		
		$expenses_arr = $arr;
		$expenses_arr['mtype'] = 1;
		$expenses = $this->cashiersModel->get_totals($expenses_arr,'expenses');
		
		$ls_num = $income - $expenses;
		$ls_num = toFloat($ls_num * $exchange_rate);
		return $ls_num;
	}
	
	
	function get_company_bumen(){
		
		$company = $this->input->post('company',null,array());
		$bumen = $this->input->post('bumen',null,'');

		$companys = explode(',',$company);
		
		if(count($companys)<=0){
			$this->err_out('请选择主体公司');
		}
		
		if(empty($bumen)){
			$bumen = $this->admin['bumen'];
		}
		if(!is_array($bumen)){
			$bumen = explode(',',$bumen);
		}
		$bumen = array_filter($bumen);
		$companys = array_filter($companys);
		
		$this->load->model('Chuna/cashiersModel','cashiersModel');
		
		$arr = array();
		foreach($companys as $val){
			foreach($bumen as $bm){
				$income_total = $expenses_total = '';
				$income_total = $this->cashiersModel->get_totals(array('mtype'=>0,'company'=>$val,'bumen'=>$bm),'income');
				$expenses_total = $this->cashiersModel->get_totals(array('mtype'=>1,'company'=>$val,'bumen'=>$bm),'expenses');
				$arr[] = array(
					'company'=>$val,
					'bumen'=>$bm,
					//'data'=>array()
					'income'=>$income_total,
					'expenses'=>$expenses_total,
				);
			}
		}
		
		$this->data_out($arr);
	}
	
}
